using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace CreatingRainbows
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameScene : DrawableGameComponent
    {
        private readonly List<GameComponent> components;
        public List<GameComponent> Components
        {
            get { return components; }
        }

        public Player main;

        bool isPlayer;

        Texture2D mainImg;

        SpriteBatch spriteBatch;

        public Game1 mainReference;

        public List<Vector2> enteranceLocs;

        public GameScene(Game1 game, bool isPlayer = true)
            : base(game)
        {
            this.isPlayer = isPlayer;
            mainReference = game;
            //mainReference.Components.Add(this);
            components = new List<GameComponent>();
            Visible = false;
            Enabled = false;
            // TODO: Construct any child components here
        }

        public virtual void Show()
        {
            Visible = true;
            Enabled = true;
        }

        public virtual void Hide()
        {
            Visible = false;
            Enabled = false;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {

            main = new Player(mainReference, this, isPlayer);

            main.Initialize(Vector2.Zero, new Rectangle(0, 0, 25, 25), Color.White,
                            Vector2.Zero, new Vector2(1, 1), 0.0f);
            // TODO: Add your initialization code here

            base.Initialize();

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void LoadContent()
        {
            if (isPlayer)
            {
                mainImg = mainReference.Content.Load<Texture2D>("player");
                main.LoadContent(spriteBatch, ref mainImg);
            }
            else
            {
                mainImg = mainReference.Content.Load<Texture2D>("cursor");
                main.LoadContent(spriteBatch, ref mainImg);
            }

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            for (int i = 0; i < components.Count; i++)
            {
                if (components[i].Enabled)
                {
                    components[i].Update(gameTime);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.Black);

            for (int i = 0; i < components.Count; i++)
            {
                GameComponent gc = components[i];
                if ((gc is DrawableGameComponent) &&
                    ((DrawableGameComponent) gc).Visible)
                {
                    ((DrawableGameComponent)gc).Draw(gameTime);
                }
            }

            base.Draw(gameTime);
        }

        public Vector2 Localize(float x, float y)
        {
            return new Vector2(mainReference.ForegroundX + x, mainReference.ForegroundY + y);
        }

        public virtual void CheckGoals()
        {
        }
    }
}
